package org.dizem.groupon.action;

import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.Random;

/**
 * User: dizem@126.com
 * Time: 11-6-23 下午10:38
 */

public class ValidationCodeAction extends ActionSupport
		implements ServletResponseAware, ServletRequestAware {

	public static final String CODE_CHARS =
			"23456789abcdefghijklmnpqrstuvwxyzABCDEFGHIJKLMNPQRSTUVWXYZ";

	public static final int IMG_WIDTH = 90;

	public static final int IMG_HEIGHT = 35;

	public static final Font[] IMG_FONTS = {
			new Font("Times New Roman", Font.ITALIC, IMG_HEIGHT - 3),
			new Font("Arial", Font.ITALIC, IMG_HEIGHT - 3),
			new Font("Consolas", Font.ITALIC, IMG_HEIGHT - 3)
	};

	private static final Color[] COLORS = {
			new Color(0xFFB90F), new Color(0x9F79EE), new Color(0x87CEEB),
			new Color(0x00CD66), new Color(0x8B7500), new Color(0xFA8072),
			new Color(0x8B8B00)
	};

	private HttpServletRequest request;
	private HttpServletResponse response;
	private String validationCode;

	public void setServletRequest(HttpServletRequest httpServletRequest) {
		this.request = httpServletRequest;
	}

	public void setServletResponse(HttpServletResponse httpServletResponse) {
		this.response = httpServletResponse;
	}

	/**
	 * generate an image of validation code
	 *
	 * @return an image of validation code
	 */
	private BufferedImage generateImage() {
		BufferedImage image = new BufferedImage(IMG_WIDTH, IMG_HEIGHT, BufferedImage.TYPE_INT_RGB);
		Graphics g = image.getGraphics();
		((Graphics2D) g).setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

		Random random = new Random();
		g.setColor(new Color(0xf0f3f8));
		g.fillRect(0, 0, IMG_WIDTH, IMG_HEIGHT);
		StringBuilder code = new StringBuilder(4);

		for (int i = 0; i < 4; ++i) {

			g.setColor(COLORS[random.nextInt(COLORS.length)]);
			int index = random.nextInt(CODE_CHARS.length());

			code.append(CODE_CHARS.charAt(index));
			g.setFont(IMG_FONTS[random.nextInt(IMG_FONTS.length)]);
			g.drawString("" + CODE_CHARS.charAt(index), 5 + 16 * i + random.nextInt(5), IMG_HEIGHT - random.nextInt(5) - 3);
		}

		g.dispose();
		validationCode = code.toString();
		return image;
	}

	@Override
	public String execute() throws Exception {
		response.setHeader("ragma", "No-cache");
		response.setHeader("Cache-Control", "no-cache");
		response.setDateHeader("Expires", 0);
		ImageIO.write(generateImage(), "JPEG", response.getOutputStream());

		HttpSession session = request.getSession();
		session.setMaxInactiveInterval(5 * 60);
		session.setAttribute("validation_code", validationCode);
		return null;
	}
}
